/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac4.client.conexion;
import edu.uoc.tdp.pac4.util.LanguageUtils;
import edu.uoc.tdp.pac4.beans.Grupo;
import javax.swing.JOptionPane;
import java.io.IOException;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.net.URL;

/**
 *
 * @author eSupport Netbeans
 */
public class PnlMuestraBibliografia extends javax.swing.JDialog implements HyperlinkListener  {

    private Grupo grupo;
    private LanguageUtils language;    
    /**
     * Creates new form PnlMuestraPlanDocente
     */
    public PnlMuestraBibliografia(PnlSolicitaMatricula parent, boolean modal, LanguageUtils language, Grupo grupo) {
        
        super(parent, modal);
        this.language = language;
        this.grupo = grupo;        
        initComponents();        
        inicializaEditorPanes();        
    }

    /**
     * Método que inicializa los jEditorPane.
     */
    private void inicializaEditorPanes()
    {
        
        this.setTitle(language.getProperty("cnxmatricula.bibliografia.title") + grupo.getNombreCurso());
        jEdtPaneBibliografia.setContentType("text/html"); 
        jEdtPaneInfoOnline.setContentType("text/html"); 
        jEdtPaneBibliografia.setText(grupo.getBibliografiaCurso());
        jEdtPaneBibliografia.addHyperlinkListener(this);
        jEdtPaneInfoOnline.addHyperlinkListener(this);
        tabBibliografia.setTitleAt(0, language.getProperty("cnxmatricula.bibliografia.tabbibliografia"));
        tabBibliografia.setTitleAt(1, language.getProperty("cnxmatricula.bibliografia.tabsolicitarlibro"));
        tabBibliografia.setToolTipText(null);
        tabBibliografia.setToolTipTextAt(0, language.getProperty("cnxmatricula.bibliografia.tooltipinternet")); 
        tabBibliografia.setToolTipTextAt(1, language.getProperty("cnxmatricula.bibliografia.tooltipinternet2")); 
        jEdtPaneBibliografia.setCaretPosition(0);
        try {            
            jEdtPaneInfoOnline.setPage("http://biblio.uoc.es/esp/index.html");            
        }
        catch (IOException ex) {
            
            //NOI18N
            JOptionPane.showMessageDialog(null, 
                language.getProperty("err.generic") + "\n" + language.getProperty("err.detail") + ":\n\n" + ex.getMessage(), 
                language.getProperty("app.title"), 
                JOptionPane.ERROR_MESSAGE);
        }
        catch (Exception ex) 
        {
             JOptionPane.showMessageDialog(null, 
                language.getProperty("err.generic") + "\n" + language.getProperty("err.detail") + ":\n\n" + ex.getMessage(), 
                language.getProperty("app.title"), 
                JOptionPane.ERROR_MESSAGE);
        }
        
    }
    
    /**
     * Método que escucha el evento click de los hipervínculos.
     * @param event 
     */
    @Override
    public void hyperlinkUpdate(HyperlinkEvent event) {
        if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            try {
                
                if (tabBibliografia.getSelectedIndex() == 0)
                {        
                    tabBibliografia.setSelectedIndex(1);
                    jEdtPaneInfoOnline.setPage("http://cataleg.uoc.edu/search*spi/?searchtype=X&searcharg=" +
                                                event.getDescription().replaceAll("\\s", "+") 
                                                + "&searchscope=1");
                }
                else
                    jEdtPaneInfoOnline.setPage(event.getURL());
                
            }catch (IOException ex) {
                
                //NOI18N
                JOptionPane.showMessageDialog(null, 
                        language.getProperty("err.generic") + "\n" + language.getProperty("err.detail") + ":\n\n" + ex.getMessage(), 
                        language.getProperty("app.title"), 
                        JOptionPane.ERROR_MESSAGE);
                
            }catch (Exception ex)
            {
                //NOI18N
                JOptionPane.showMessageDialog(null, 
                        language.getProperty("err.generic") + "\n" + language.getProperty("err.detail") + ":\n\n" + ex.getMessage(), 
                        language.getProperty("app.title"), 
                        JOptionPane.ERROR_MESSAGE); 
            }
    }
  }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cmdCerrar = new javax.swing.JButton();
        tabBibliografia = new javax.swing.JTabbedPane();
        jScrlPaneBibliografia = new javax.swing.JScrollPane();
        jEdtPaneBibliografia = new javax.swing.JEditorPane();
        jScrlPaneOnline = new javax.swing.JScrollPane();
        jEdtPaneInfoOnline = new javax.swing.JEditorPane();
        cmdRecargar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(800, 600));

        cmdCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/arrow-curve-180.png"))); // NOI18N
        cmdCerrar.setText(language.getProperty("cnxmatricula.bibliografia.cerrar"));
        cmdCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCerrarActionPerformed(evt);
            }
        });

        tabBibliografia.setToolTipText("");

        jEdtPaneBibliografia.setEditable(false);
        jScrlPaneBibliografia.setViewportView(jEdtPaneBibliografia);

        tabBibliografia.addTab("Bibliografia", jScrlPaneBibliografia);

        jEdtPaneInfoOnline.setEditable(false);
        jScrlPaneOnline.setViewportView(jEdtPaneInfoOnline);

        tabBibliografia.addTab("Solicitar Libros", jScrlPaneOnline);

        cmdRecargar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/web-slice.png"))); // NOI18N
        cmdRecargar.setText(language.getProperty("cnxmatricula.bibliografia.reload"));
        cmdRecargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdRecargarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(cmdRecargar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmdCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(tabBibliografia, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(tabBibliografia, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdRecargar, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCerrarActionPerformed
        this.dispose();
    }//GEN-LAST:event_cmdCerrarActionPerformed

    private void cmdRecargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdRecargarActionPerformed
        inicializaEditorPanes();
    }//GEN-LAST:event_cmdRecargarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdCerrar;
    private javax.swing.JButton cmdRecargar;
    private javax.swing.JEditorPane jEdtPaneBibliografia;
    private javax.swing.JEditorPane jEdtPaneInfoOnline;
    private javax.swing.JScrollPane jScrlPaneBibliografia;
    private javax.swing.JScrollPane jScrlPaneOnline;
    private javax.swing.JTabbedPane tabBibliografia;
    // End of variables declaration//GEN-END:variables
}
